package com.pt25.base.dep.nacos.rpc;

import com.pt25.base.constants.BaseConstants;
import com.pt25.base.web.WebThreadLocal;
import feign.Request;
import feign.RequestInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * rpc相关配置
 */
@Configuration
public class FeignConfig {

    @Resource
    private RpcService rpcService;

    /**
     * 进行rpc请求时，添加请求头信息
     * token: rpc验证
     * traceId: 全链路id
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            requestTemplate.header(BaseConstants.X_TRACE_ID, WebThreadLocal.get().getTraceId());
            requestTemplate.header(BaseConstants.X_RPC_TOKEN, rpcService.getToken());
        };
    }

    /**
     * 设置rpc请求超时时间
     */
    @Bean
    public Request.Options options() {
        return new Request.Options(
                Duration.ofMillis(5000), // 连接超时(毫秒)
                Duration.ofSeconds(10),// 读取超时(秒)
                true
        );
    }
}

